﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================

using System;
using System.Collections.Generic;
using System.Runtime.Caching;
using System.Windows.Forms;
using XCI.Config;
using XCI.Core;
using XCI.Extensions;
using XCI.Helper;
using XCI.Sys.Forms;
using XCI.Sys.Model;
using XCI.Sys.Service;

namespace XCI.Sys
{
    /// <summary>
    /// 应用程序运行时数据
    /// </summary>
    public static class SysRuntime
    {
        private static readonly IConfigProvider reportPrinterConfig = new JsonConfigProvider(IOHelper.GetRootFilePath("printer.json"));

        /// <summary>
        /// 启动参数
        /// </summary>
        public static string[] StartupArgs { get; set; }

        /// <summary>
        /// 当前应用程序对象
        /// </summary>
        public static IApp App { get; set; }

        /// <summary>
        /// 服务器名称
        /// </summary>
        public static string ServerName { get; set; }

        /// <summary>
        /// 应用程序设置
        /// </summary>
        public static WinFormSetting Setting { get; set; }

        /// <summary>
        /// 登陆窗口
        /// </summary>
        public static Form LoginForm { get; set; }

        /// <summary>
        /// 主窗口
        /// </summary>
        public static Form MainForm { get; set; }

        /// <summary>
        /// 启动屏幕类型
        /// </summary>
        public static Type SplashScreenType { get; set; }

        /// <summary>
        /// 加载等待窗体类型
        /// </summary>
        public static Type WaitFormType { get; set; }

        /// <summary>
        /// 业务接口操作对象
        /// </summary>
        public static WebApi Api { get; set; }

        /// <summary>
        /// 当前应用程序登陆用户
        /// </summary>
        public static SysUser CurrentUser { get; set; }

        /// <summary>
        /// 报表打印机配置文件
        /// </summary>
        public static IConfigProvider ReportPrinterProvider => reportPrinterConfig;

        /// <summary>
        /// 报表打印机
        /// </summary>
        public static Dictionary<string, string> ReportPrinter => reportPrinterConfig.Get("Printer", () => new Dictionary<string, string>());

        /// <summary>
        /// 添加主页面文档
        /// </summary>
        /// <param name="code">编码</param>
        /// <param name="form">窗体</param>
        public static void AddTabDocument(string code, Form form)
        {
            ((FrmStandardMain)MainForm).AddDocument(code, form);
        }

        /// <summary>
        /// 刷新用户模块列表
        /// </summary>
        public static void RefreshUserModules()
        {
            var list = App.GetModules(CurrentUser);
            MemoryCache.Default.Set(SysConst.UserModuleListCacheKey, list, DateTimeOffset.Now.AddHours(1));
        }

        /// <summary>
        /// 获取用户拥有的模块列表
        /// </summary>
        public static IList<SysModule> GetUserModules()
        {
            var cacheList = MemoryCache.Default.Get(SysConst.UserModuleListCacheKey);
            if (cacheList == null)
            {
                RefreshUserModules();
                cacheList = MemoryCache.Default.Get(SysConst.UserModuleListCacheKey);
            }
            if (cacheList != null)
            {
                return (IList<SysModule>)cacheList;
            }
            return new List<SysModule>();
        }

        /// <summary>
        /// 是否有指定模块编码的权限
        /// </summary>
        /// <param name="codes">模块编码集合</param>
        /// <returns>存在返回true,否则返回false</returns>
        public static bool IsAuthorize(params string[] codes)
        {
            if (CurrentUser == null) return true;
            if (CurrentUser.Admin.IfTrue()) return true;
            if (codes == null || codes.Length == 0) return false;
            var cacheObject = MemoryCache.Default.Get(SysConst.UserModuleMapCacheKey);
            if (cacheObject == null)
            {
                var list = GetUserModules();
                var map = new Dictionary<string, SysModule>(list.Count);
                foreach (var entity in list)
                {
                    map[entity.Code] = entity;
                }
                MemoryCache.Default.Set(SysConst.UserModuleMapCacheKey, map, DateTimeOffset.Now.AddHours(1));
                cacheObject = map;
            }

            var permissionMap = (Dictionary<string, SysModule>)cacheObject;
            if (permissionMap.Count == 0) return false;
            foreach (var code in codes)
            {
                var has = permissionMap.ContainsKey(code);
                if (!has)
                {
                    //任何一个为false,则直接返回false;
                    return false;
                }
            }
            return true;
        }
    }
}